#include "game.h"

int game(){
    int uuuu = 0;
    sf::RenderWindow window(sf::VideoMode(800, 400), "quotidianus");

	sf::View view(sf::FloatRect(0, 0, 800, 400));

	Level lvl;
	lvl.LoadFromFile("files/deaddrawing.tmx");

	sf::Texture t, td, bg, enemy, deadscreen, endscr, endscr1;
	bg.loadFromFile("files/Background.png");
	t.loadFromFile("files/spritesheet.png");
	enemy.loadFromFile("files/goose.png");
    deadscreen.loadFromFile("files/ohno.png");
    endscr.loadFromFile("files/enddevil.png");
    endscr1.loadFromFile("files/endrizz.png");


	AnimationManager anim, anima, animdev, ac;
	anim.loadFromXML("files/hero.xml", t);
	anim.animList["jump"].loop = 0;

	anima.create("move", enemy, 10, 10, 16, 16, 1, 0);
	anima.create("fulldead", enemy, 46, 46, 16, 16, 1, 0);
	anima.create("dobrogoose", enemy, 49, 11, 16, 16, 1, 0);

    sf::Sprite background(bg);
    background.setOrigin(bg.getSize().x/2,bg.getSize().y/2);
    sf::Sprite bl(deadscreen);
    bl.setOrigin(deadscreen.getSize().x/2,bg.getSize().y/2);
    sf::Sprite b1(endscr);
    b1.setOrigin(endscr.getSize().x/2,bg.getSize().y/2);
    sf::Sprite b2(endscr1);
    b2.setOrigin(endscr1.getSize().x/2,bg.getSize().y/2);

    std::list<sinner*>  entities;
	std::list<sinner*>::iterator it;

    std::vector<Object> e = lvl.GetObjects("enemy");
    for (int i=0;i < e.size();i++){
        entities.push_back(new sinner(anima, lvl, e[i].rect.left, e[i].rect.top) );
    }

    Player p(anim, lvl);

    sf::Music music, musicidiot, sad, intheend;
    music.openFromFile("files/russk.wav");
    musicidiot.openFromFile("files/idiot.wav");
    intheend.openFromFile("files/intheend.wav");
    music.setLoop(true);
    music.setVolume(10.f);
    musicidiot.setLoop(true);
    musicidiot.setVolume(10.f);
    intheend.setLoop(true);
    intheend.setVolume(10.f);
    sad.openFromFile("files/sad.wav");
    sad.setLoop(true);
    sad.setVolume(10.f);
    music.play();

	sf::Clock clock;

    int deadtime = 0;
    int carrottime = 0;
    int cg = 0;

    
	while (window.isOpen() && uuuu == 0){
        
        float time = clock.getElapsedTime().asMicroseconds();
        clock.restart();

        time = time/500; 
        if (time > 40) time = 40;

		sf::Event event;
		while (window.pollEvent(event)){
			if (event.type == sf::Event::Closed){
                window.close();
                uuuu = 0;
            }
            if (event.type == sf::Event::KeyPressed){
				if (event.key.code == sf::Keyboard::Escape){
		        	window.close();
                    uuuu = 0;
                }
            }
            if (event.type == sf::Event::KeyPressed){
				if (event.key.code == sf::Keyboard::Enter){
		        	uuuu = 1;
                }
            }
		}

		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) p.key["L"]=true;
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) p.key["R"]=true;
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) p.key["Up"]=true;
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) p.key["Down"]=true;
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) p.key["Space"]=true;
        if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Space) || !p.life || p.gg){
            p.key["R"] = p.key["L"] = p.key["Up"] = p.key["Down"] = p.key["Space"] = false;
        }

        for(it=entities.begin();it!=entities.end();){
            miscellanea *b = *it;
            b->update(time);
            if (b->life == false)	{ 
                it  = entities.erase(it); 
                delete b;
            }
            else it++;
		}
		p.update(time);


		for(it=entities.begin();it!=entities.end();it++){
			if ((*it)->Name == "sinner"){
				sinner *enemy = *it;

				if (enemy->Health<=0) continue;
                if (p.pray && abs(p.x - enemy->x) < 100) {
                    enemy->dx=0; 
                    enemy->joke = true;
                    cg++;
                }
                if (p.attack && abs(p.x - enemy->x) < 30) {
                    enemy->dx=0; 
                    enemy->Health=0;
                    p.devil = true;
                    
                    if (deadtime == 0){
                        music.stop();
                        musicidiot.play();
                        deadtime = 1;
                    } 
                }

				if  (p.getRect().intersects(enemy->getRect())){
				    if (!p.hit && !enemy->joke) { 
                        p.life = false; 
                        p.hit=true;
				        if (p.dir) p.x+=10; 
                        else p.x-=10;
                    }
                }
            }
		}

        view.setCenter(p.x,200);
        window.setView(view);

        background.setPosition(view.getCenter());
        bl.setPosition(p.x, 200);
        b1.setPosition(p.x, 200);
        b2.setPosition(p.x, 200);
        window.draw(background);

        lvl.Draw(window);

        for(it=entities.begin();it!=entities.end();it++){
            (*it)->draw(window);
        }

        p.draw(window);

        if (!p.anim.isPlaying() && !p.life && deadtime < 2){            
            if(deadtime == 0) music.stop();
            if(deadtime == 1) musicidiot.stop();
            deadtime = 2;
            sad.play();
        }

        if(deadtime == 2){ 
            window.draw(bl); 
        }

        if (!p.anim.isPlaying() && p.gg && carrottime == 0){            
            if(!p.devil) music.stop();
            if(p.devil) musicidiot.stop();
            carrottime = 1;
            intheend.play();
        }

        if(carrottime == 1){ 
            if (p.devil) window.draw(b1); 
            if (!p.devil) window.draw(b2);
        }

        window.display();
    }
    return uuuu;
}